#include <bits/stdc++.h>

#define in read<int>()
#define lin read<ll>()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1
//#define int long long
using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

template <typename T>
T read(){T x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 2e5+10;
const int L = 2e6+10;

int ch[L][2],ndn,n,m,tmp;
int rt[N],tot;
ll s[L],a[N];

namespace pool{
	int stk[L],top;
	inline int newd(){return top ? stk[top--] : ++ndn;}
	inline void del(int x){stk[++top] = x;ch[x][0] = ch[x][1] = s[x] = 0;}
}

using pool :: newd;
using pool :: del;

void merge(int &y,int x){
	if(!x || !y) return y = x | y,void();
	merge(ch[y][0],ch[x][0]); merge(ch[y][1],ch[x][1]);
	s[y] += s[x]; del(x);
}

void split(int x,int &y,ll k){
	if(x == 0) return; y = newd(); ll v = s[ch[x][0]];
	if(k > v) split(ch[x][1],ch[y][1],k - v);
	else swap(ch[x][1],ch[y][1]);
	if(k < v) split(ch[x][0],ch[y][0],k);
	s[y] = s[x] - k; s[x] = k;
}

void upd(int &y,int ps,ll v,int l = 1,int r = n){
	if(!y) y = newd(); s[y] += v; if(l == r) return; int mid = l + r >> 1;
	if(ps <= mid) upd(ch[y][0],ps,v,l,mid); else upd(ch[y][1],ps,v,mid + 1,r);
}

ll query(int y,int xl,int xr,int l = 1,int r = n){
	if(!y) return 0; if(xl == l && xr == r) return s[y]; int mid = l + r >> 1;
	if(xr <= mid) return query(ch[y][0],xl,xr,l,mid); else if(xl > mid) return query(ch[y][1],xl,xr,mid + 1,r);
	else return query(ch[y][0],xl,mid,l,mid) + query(ch[y][1],mid + 1,xr,mid + 1,r);
}

int kth(int y,ll k,int l = 1,int r = n){
	if(l == r) return l; int mid = l + r >> 1;
	if(s[ch[y][0]] < k) return kth(ch[y][1],k - s[ch[y][0]],mid + 1,r); else return kth(ch[y][0],k,l,mid);
}

void build(int &x,int l = 1,int r = n){
	x = newd(); if(l == r) return s[x] = a[l],void(); int mid = l + r >> 1;
	build(ch[x][0],l,mid); build(ch[x][1],mid+1,r); s[x] = s[ch[x][0]] + s[ch[x][1]];
}

signed main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in;
	for(int i = 1;i <= n;i++) a[i] = lin;
	build(rt[1]); tot = 1; static int tt = 0;
	while(m--){
		int op = in,p = in,y; ll x = lin; tt++; if(tt <= 20) cerr<<op<<endl;
		if(op == 0){
			y = in; ll n1 = query(rt[p],x,y),n2 = query(rt[p],1,y);
			split(rt[p],rt[++tot],n2 - n1); split(rt[tot],tmp,n1); merge(rt[p],tmp);
		}else if(op == 1) merge(rt[p],rt[x]);
		else if(op == 2) y = in,upd(rt[p],y,x);
		else if(op == 3) y = in,printf("%lld\n",query(rt[p],x,y));
		else printf("%d\n",s[rt[p]] < x ? -1 : kth(rt[p],x));
	}
	return 0;
}
